package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealDIshService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    private final SetmealDIshService setmealDIshService;

    @Override
    public PageResult selectSetMealPage(SetmealPageQueryDTO dto) {
        Integer page = dto.getPage();
        Integer pageSize = dto.getPageSize();
        if (ObjectUtil.isNull(page) || ObjectUtil.isNull(pageSize)) {
            throw new BaseException(MessageConstant.PAGE_PARAM_NO_NULL);
        }


        Page<SetmealVO> setmealPage = new Page<>(page, pageSize);
        baseMapper.selectSetMealPage(setmealPage, dto);
        return PageResult.builder().total(setmealPage.getTotal()).records(setmealPage.getRecords()).build();
    }

    @Override
    @Transactional
    public void add(SetmealDTO dto) {
        //新增套餐时需要添加套餐所包含的菜品(setmeal_dish中间表)
        //1.新增套餐
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        setmeal.setStatus(0);
        save(setmeal);
        //2.新增套餐所包含的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            setmealDIshService.saveBatch(setmealDishes);
        }

    }

    @Override
    public SetmealVO selectSetmealById(Long id) {
        SetmealVO setmealVO =baseMapper.selectSetmealById(id);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO dto) {
        //修改套餐时需要修改套餐所包含的菜品(setmeal_dish中间表)
        //1.修改套餐
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        updateById(setmeal);
        //2.修改套餐所包含的菜品
        //删除套餐所包含的菜品
        setmealDIshService.remove(Wrappers.<SetmealDish>lambdaQuery().eq(SetmealDish::getSetmealId, setmeal.getId()));
        //添加套餐所包含的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
            setmealDIshService.saveBatch(setmealDishes);
//            Db.saveBatch(setmealDishes);
        }
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //删除套餐时需要删除套餐所包含的菜品(setmeal_dish中间表)
        //1.批量删除套餐
        removeBatchByIds(ids);
        //2.批量删除套餐所包含的菜品
        setmealDIshService.remove(Wrappers.<SetmealDish>lambdaQuery().in(SetmealDish::getSetmealId, ids));
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        if (status == 0){
            lambdaUpdate().set(Setmeal::getStatus,0).eq(Setmeal::getId,id).update();
        }else if (status == 1){
            //如果套餐还没有绑定菜品，则不能起售
            long count = setmealDIshService.count(Wrappers.<SetmealDish>lambdaQuery().eq(SetmealDish::getSetmealId, id));
            if (count == 0){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
            lambdaUpdate().set(Setmeal::getStatus,1).eq(Setmeal::getId,id).update();
        }



    }
}
